package routers

import (
	"fmt"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"secure-sys/handlers"
	"secure-sys/pkg"
)

func MiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {
		tokenString := c.Request.Header.Get("token")
		if tokenString == "" {
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "token为空",
			})
			c.Abort()
		}
		getToken, err := pkg.GetToken(tokenString)
		if err != "" {
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "token解密失败",
			})
			c.Abort()
		}
		fmt.Println("解析完token参数后:", getToken)

		c.Set("uid", getToken["uid"])
		c.Next()
	}
}

func Init() *gin.Engine {
	r := gin.Default()
	r.Use(cors.Default())
	r.Static("static", "./static")

	r.POST("/user/add", handlers.UserLogin)

	return r
}
